package srzy.module

import java.io.File

object ReadCdrFile extends AbsCaseDeal with srzy.common.Logging {
  def deal = {

    logger.debug("read Cdr File")
    val ncd = normalCdrDir.map(new File(_))
    val ecd = errCdrDir.map(new File(_))
    val tcd = transCdrDir.map(new File(_))

    var i = 0
    if (getCaseInfo.normalCdr.stdFull.length +
      getCaseInfo.errCdr.stdFull.length +
      getCaseInfo.transCdr.stdFull.length == 0) {
      Thread.sleep(1000)
    } else {
      val llll = ncd ::: ecd ::: tcd ::: Nil
      while ((llll.flatMap(_.listFiles()).filter(_.getName().takeRight(3) != "tmp").length == 0) & (i < 1000000)) {
        Thread.sleep(1000)
        i = i + 1
        logger.debug("wait for cdr file " + i + "s")
      }
    }

    Thread.sleep(1000)

    val ncdl = ncd.flatMap(_.listFiles())
    val ecdl = ecd.flatMap(_.listFiles())
    val tcdl = tcd.flatMap(_.listFiles())
    logger.debug("normal cdr file :" + ncdl.map(_.getAbsolutePath()).mkString(" "))
    //    logger.debug(ncdl.map(x => {
    //      logger.debug("FileName:" + x.getAbsolutePath())
    //      logger.debug("FileText:" + srzy.common.Common.readFromFile(x.getAbsolutePath()))
    //    }))
    logger.debug("err cdr file :" + ncdl.map(_.getAbsolutePath()).mkString(" "))
    //    logger.debug(ncdl.map(x => {
    //      logger.debug("FileName:" + x.getAbsolutePath())
    //      logger.debug("FileText:" + srzy.common.Common.readFromFile(x.getAbsolutePath()))
    //    }))
    logger.debug("trans cdr file :" + ncdl.map(_.getAbsolutePath()).mkString(" "))
    //    logger.debug(ncdl.map(x => {
    //      logger.debug("FileName:" + x.getAbsolutePath())
    //      logger.debug("FileText:" + srzy.common.Common.readFromFile(x.getAbsolutePath()))
    //    }))

    if (ncdl.length > 1) {
      throw new Exception("normal cdr file :" + ncdl.map(_.getAbsolutePath()).mkString(" ") + " too much")
    }
    if (ecdl.length > 1) {
      throw new Exception("err cdr file :" + ecdl.map(_.getAbsolutePath()).mkString(" ") + " too much")
    }
    if (tcdl.length > 1) {
      throw new Exception("trans cdr file :" + tcdl.map(_.getAbsolutePath()).mkString(" ") + " too much")
    }

    if (ncdl.length == 1) {
      getCaseInfo.normalCdr.realFull =
        srzy.common.Common.readFromFile(ncdl.apply(0).getAbsolutePath())
    }
    if (ecdl.length == 1) {
      getCaseInfo.errCdr.realFull =
        srzy.common.Common.readFromFile(ecdl.apply(0).getAbsolutePath())
    }
    if (tcdl.length == 1) {
      getCaseInfo.transCdr.realFull =
        srzy.common.Common.readFromFile(tcdl.apply(0).getAbsolutePath())
    }
  }
}